package com.eleadmin.order.biz.impl;

import com.eleadmin.common.system.entity.User;
import com.eleadmin.damon.utils.constant.UserConstant;
import com.eleadmin.damon.utils.results.BaseDao;
import com.eleadmin.damon.utils.results.PageResult2;
import com.eleadmin.damon.utils.utils.Utils;
import com.eleadmin.order.biz.WriteOffBiz;
import com.eleadmin.order.entity.WriteOff;
import com.eleadmin.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 * 数据层实现类
 *
 * @author : Doman
 * @version ：1.0
 * @date : 2023-08-11 16:47:23
 */
@Repository
public class WriteOffBizImpl extends BaseDao implements WriteOffBiz {

    @Override
    public JsonResult list(PageResult2<T> page, WriteOff writeOff, User loginUser) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<WriteOff> rm = new BeanPropertyRowMapper<WriteOff>(WriteOff.class);
        StringBuffer sql = new StringBuffer("select w.id,w.organization_id,w.customer_id");
        sql.append(",w.customer,w.w_date,w.w_price,w.invoice_price");
        sql.append(",w.type,w.actual_price, w.classify");
        sql.append(",w.w_price_usd,w.invoice_price_usd,w.actual_price_usd");
        sql.append(",w.bill_ids,w.order_num,w.create_time,w.create_user");
        sql.append(",w.remark,w.finance_remark,w.state,w.paid_state");
        sql.append(",w.update_time,w.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName,suser2.nickname updateName ");
        sql.append(" from chf_write_off w ");
        sql.append(" inner join sys_user suser on suser.user_id=w.create_user ");
        sql.append(" left join sys_user suser2 on suser2.user_id=w.update_user ");
        if (Utils.isNotEmpty(writeOff)) {
            if (Utils.isNotEmpty(writeOff.getSellsId())) {
                sql.append(" inner join chf_customer cu on cu.id=w.customer_id ");
            }
        }
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=w.organization_id and sogzn.deleted=0 ");
        sql.append(" where w.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and w.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(writeOff)) {
            if (Utils.isNotEmpty(writeOff.getSellsId())) {
                sql.append(" and cu.salesmanId=:salesmanId ");
                map.put("salesmanId", writeOff.getSellsId());
            }
            if (Utils.isNotEmpty(writeOff.getCustomer())) {
                sql.append(" and instr(w.customer,:customer)>0 ");
                map.put("customer", writeOff.getCustomer());
            }
            if (Utils.isNotEmpty(writeOff.getwDate())) {
                sql.append(" and w.w_date=:w_date ");
                map.put("w_date", writeOff.getwDate());
            }
            if (Utils.isNotEmpty(writeOff.getDate1())) {
                sql.append(" and w.w_date>=:date1 ");
                map.put("date1", writeOff.getDate1() + " 00:00");
            }
            if (Utils.isNotEmpty(writeOff.getDate2())) {
                sql.append(" and w.w_date<=:date2 ");
                map.put("date2", writeOff.getDate2() + " 23:59");
            }
            if (Utils.isNotEmpty(writeOff.getType())) {
                sql.append(" and instr(w.type,:type)>0 ");
                map.put("type", writeOff.getType());
            }
            if (Utils.isNotEmpty(writeOff.getClassify())) {
                sql.append(" and instr(w.classify,:classify)>0 ");
                map.put("classify", writeOff.getClassify());
            }
            if (Utils.isNotEmpty(writeOff.getState())) {
                sql.append(" and instr(w.state,:state)>0 ");
                map.put("state", writeOff.getState());
            }
            if (Utils.isNotEmpty(writeOff.getPaidState())) {
                sql.append(" and instr(w.paid_state,:paid_state)>0 ");
                map.put("paid_state", writeOff.getPaidState());
            }
            if (Utils.isNotEmpty(writeOff.getRemark())) {
                sql.append(" and instr(w.remark,:remark)>0 ");
                map.put("remark", writeOff.getRemark());
            }
            if (Utils.isNotEmpty(writeOff.getSort()) && Utils.isNotEmpty(writeOff.getOrder())) {
                sql.append(" order by w." + writeOff.getSort() + " " + writeOff.getOrder());
            } else {
                sql.append(" order by w.create_time desc");
            }
        }
        return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
    }

    @Override
    public List<WriteOff> list(WriteOff writeOff, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<WriteOff> rm = new BeanPropertyRowMapper<WriteOff>(WriteOff.class);
        StringBuffer sql = new StringBuffer("select w.id,w.organization_id,w.customer_id");
        sql.append(",w.customer,w.w_date,w.w_price,w.invoice_price");
        sql.append(",w.type,w.actual_price, w.classify");
        sql.append(",w.w_price_usd,w.invoice_price_usd,w.actual_price_usd");
        sql.append(",w.bill_ids,w.order_num,w.create_time,w.create_user");
        sql.append(",w.remark,w.finance_remark,w.state,w.paid_state");
        sql.append(",w.update_time,w.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from chf_write_off w ");
        sql.append(" inner join sys_user suser on suser.user_id=w.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=w.organization_id and sogzn.deleted=0 ");
        sql.append(" where w.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and w.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(writeOff)) {

            if (Utils.isNotEmpty(writeOff.getCustomer())) {
                sql.append(" and instr(w.customer,:customer)>0 ");
                map.put("customer", writeOff.getCustomer());
            }
            if (Utils.isNotEmpty(writeOff.getwDate())) {
                sql.append(" and w.w_date=:w_date ");
                map.put("w_date", writeOff.getwDate());
            }
            if (Utils.isNotEmpty(writeOff.getDate1())) {
                sql.append(" and w.w_date>=:date1 ");
                map.put("date1", writeOff.getDate1() + " 00:00");
            }
            if (Utils.isNotEmpty(writeOff.getDate2())) {
                sql.append(" and w.w_date<=:date2 ");
                map.put("date2", writeOff.getDate2() + " 23:59");
            }
            if (Utils.isNotEmpty(writeOff.getType())) {
                sql.append(" and instr(w.type,:type)>0 ");
                map.put("type", writeOff.getType());
            }
            if (Utils.isNotEmpty(writeOff.getClassify())) {
                sql.append(" and instr(w.classify,:classify)>0 ");
                map.put("classify", writeOff.getClassify());
            }
            if (Utils.isNotEmpty(writeOff.getState())) {
                sql.append(" and instr(w.state,:state)>0 ");
                map.put("state", writeOff.getState());
            }
            if (Utils.isNotEmpty(writeOff.getPaidState())) {
                sql.append(" and instr(w.paid_state,:paid_state)>0 ");
                map.put("paid_state", writeOff.getPaidState());
            }
            if (Utils.isNotEmpty(writeOff.getRemark())) {
                sql.append(" and instr(w.remark,:remark)>0 ");
                map.put("remark", writeOff.getRemark());
            }
            if (Utils.isNotEmpty(writeOff.getSort()) && Utils.isNotEmpty(writeOff.getOrder())) {
                sql.append(" order by w." + writeOff.getSort() + " " + writeOff.getOrder());
            } else {
                sql.append(" order by w.create_time desc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(), map, rm);
    }

    @Override
    public WriteOff getById(String id) {
        RowMapper<WriteOff> rm = new BeanPropertyRowMapper<WriteOff>(WriteOff.class);
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select w.id,w.organization_id,w.customer_id");
        sql.append(",w.customer,w.w_date,w.w_price,w.invoice_price");
        sql.append(",w.type,w.actual_price, w.classify");
        sql.append(",w.w_price_usd,w.invoice_price_usd,w.actual_price_usd");
        sql.append(",w.bill_ids,w.order_num,w.create_time,w.create_user");
        sql.append(",w.remark,w.finance_remark,w.state,w.paid_state");
        sql.append(",w.update_time,w.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from chf_write_off w ");
        sql.append(" inner join sys_user suser on suser.user_id=w.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=w.organization_id and sogzn.deleted=0 ");
        sql.append(" where w.deleted=0 and w.id=:id ");
        map.put("id", id);
        getMapParameter(sql, map);
        return jdbcTemplate.queryForObject(sql.toString(), map, rm);
    }

    @Override
    public boolean save(WriteOff writeOff, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("insert into chf_write_off (");
        sql.append("organization_id,customer_id,customer,w_date");
        sql.append(",w_price,invoice_price,bill_ids,order_num");
        sql.append(",type,actual_price,classify");
        sql.append(",w_price_usd,invoice_price_usd,actual_price_usd");
        sql.append(",remark,finance_remark,state,paid_state");
        sql.append(",create_user,create_time) values ( ");
        sql.append(":organization_id,:customer_id,:customer,:w_date");
        sql.append(",:w_price,:invoice_price,:bill_ids,:order_num");
        sql.append(",:type,:actual_price,:classify");
        sql.append(",:w_price_usd,:invoice_price_usd,:actual_price_usd");
        sql.append(",:remark,:finance_remark,:state,:paid_state");
        sql.append(",:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("customer_id", writeOff.getCustomerId() == null ? null : writeOff.getCustomerId() + "");
        map.put("customer", writeOff.getCustomer());
        map.put("w_date", writeOff.getwDate());
        map.put("w_price", writeOff.getwPrice());
        map.put("remark", writeOff.getRemark());
        map.put("type", writeOff.getType());
        map.put("finance_remark", writeOff.getFinanceRemark());
        map.put("state", writeOff.getState());
        map.put("paid_state", writeOff.getPaidState());
        map.put("invoice_price", writeOff.getInvoicePrice());
        map.put("actual_price", writeOff.getActualPrice());
        map.put("classify", writeOff.getClassify());
        map.put("w_price_usd", writeOff.getwPriceUsd());
        map.put("invoice_price_usd", writeOff.getInvoicePriceUsd());
        map.put("actual_price_usd", writeOff.getActualPriceUsd());
        map.put("bill_ids", writeOff.getBillIds());
        map.put("order_num", writeOff.getOrderNum() == null ? null : writeOff.getOrderNum() + "");
        map.put("create_user", loginUser.getUserId() + "");
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean update(WriteOff writeOff, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("update chf_write_off set ");
        sql.append("customer_id=:customer_id,customer=:customer,w_date=:w_date ");
        sql.append(",w_price=:w_price,invoice_price=:invoice_price,bill_ids=:bill_ids ");
        sql.append(",type=:type,actual_price=:actual_price,classify=:classify");
        sql.append(",w_price_usd=:w_price_usd,invoice_price_usd=:invoice_price_usd,actual_price_usd=:actual_price_usd");
        sql.append(",remark=:remark,finance_remark=:finance_remark,state=:state,paid_state=:paid_state ");
        sql.append(",order_num=:order_num,update_time=now(),update_user=:update_user where id=:id ");
        map.put("customer_id", writeOff.getCustomerId() == null ? null : writeOff.getCustomerId() + "");
        map.put("customer", writeOff.getCustomer());
        map.put("w_date", writeOff.getwDate());
        map.put("w_price", writeOff.getwPrice());
        map.put("invoice_price", writeOff.getInvoicePrice());
        map.put("actual_price", writeOff.getActualPrice());
        map.put("classify", writeOff.getClassify());
        map.put("w_price_usd", writeOff.getwPriceUsd());
        map.put("invoice_price_usd", writeOff.getInvoicePriceUsd());
        map.put("actual_price_usd", writeOff.getActualPriceUsd());
        map.put("bill_ids", writeOff.getBillIds() == null ? null : writeOff.getBillIds() + "");
        map.put("remark", writeOff.getRemark());
        map.put("finance_remark", writeOff.getFinanceRemark());
        map.put("type", writeOff.getType());
        map.put("state", writeOff.getState());
        map.put("paid_state", writeOff.getPaidState());
        map.put("order_num", writeOff.getOrderNum() == null ? null : writeOff.getOrderNum() + "");
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", writeOff.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean updateState(WriteOff writeOff, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("update chf_write_off set ");
        sql.append("state=:state ");
        sql.append(",update_time=now(),update_user=:update_user where id=:id ");
        map.put("state", writeOff.getState());
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", writeOff.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean remove(String id, User loginUser) {
        int count = 0;
        Map<String, String> map = null;
        if (id.length() > 0) {
            for (String id1 : id.split(",")) {
                map = new HashMap<String, String>();
                if (id1 == null || id1.equals("")) {
                    break;
                }
                StringBuffer sql = new StringBuffer("update chf_write_off set deleted=1" +
                        ",update_user=:update_user, update_time=now() "
                        + " where id =:id ");
                map.put("update_user", loginUser.getUserId() + "");
                map.put("id", id1);
                getMapParameter(sql, map);
                count = jdbcTemplate.update(sql.toString(), map);
            }
        }
        return count > 0;
    }
}
